﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;

namespace Frame.Lib.Utilities
{
    /// <summary>
    /// 字符串的实用类
    /// </summary>
    public sealed class StrUtil
    {

        #region 进行MD5加密
        /// <summary>
        /// 进行MD5加密
        /// </summary>
        /// <param name="strSrc"></param>
        /// <returns></returns>
        public static string EncryptMD5(string strSrc)
        {
            return Encryption.EncriptMD5(strSrc);
        }
        #endregion

        #region 用DES进行加密字符串
        /// <summary>
        /// 用DES进行加密字符串
        /// </summary>
        /// <param name="strSrc"></param>
        /// <returns></returns>
        public static string DecryptDES(string strSrc)
        {
            return Encryption.DecriptDES(Constants.DES_KEY, strSrc);
        }

        public static string DecryptDES(string strSrc, string key)
        {
            return Encryption.DecriptDES(key, strSrc);
        }
        #endregion

        #region 用DES进行解密字符串
        /// <summary>
        /// 用DES进行解密字符串   
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string EncryptDES(string src)
        {
            return Encryption.EncriptDES(Constants.DES_KEY, src);
        }

        public static string EncryptDES(string src, string key)
        {
            return Encryption.EncriptDES(key, src);
        }
        #endregion

        #region 对Url进行加密编码

        /// <summary>
        /// 对Url进行加密编码
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string EncryptUrl(object src)
        {
            string str = EncryptDES(src.ToString());
            return System.Web.HttpUtility.UrlEncode(str);
        }

        #endregion

        #region 对Url进行解密解码
        /// <summary>
        /// 对Url进行解密解码
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string DecryptUrl(object src)
        {
            if (src == null)
            {
                return null;
            }
            if (src.ToString() == "")
            {
                return "";
            }
            return DecryptDES(src.ToString());
        }
        #endregion

        #region 将string类型的用指定字符劈成List<int>类型
        /// <summary>
        /// 用,号拆分字符串
        /// </summary>
        /// <param name="strInts"></param>
        /// <returns></returns>
        public static List<int> SplitToInt(string strInts)
        {
            return SplitToInt(strInts, ',');
        }

        public static List<long> SplitToLong(string strs)
        {
            string[] strTmp = strs.Split(',');
            List<long> lst = new List<long>();
            foreach (string str in strTmp)
            {
                if (str != "")
                {
                    long value = long.Parse(str);
                    if (lst.IndexOf(value) < 0)
                    {
                        lst.Add(value);
                    }
                }
            }
            return lst;
        }

        /// <summary>
        /// 用指定字符拆分字符串
        /// </summary>
        /// <param name="strInts"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static List<int> SplitToInt(string strInts, char separator)
        {
            string[] strTmp = strInts.Split(separator);
            List<int> lstInts = new List<int>(strTmp.Length);
            foreach (string str in strTmp)
            {
                if (str != "")
                {
                    int value = int.Parse(str);
                    if (lstInts.IndexOf(value) < 0)
                    {
                        lstInts.Add(value);
                    }
                }
            }
            return lstInts;
        }
        #endregion

        #region 将string类型的用指定字符劈成List<int>类型

        /// <summary>
        /// 用指定字符拆分字符串
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static List<string> SplitToStr(string strs, char separator)
        {
            string[] strTmp = strs.Split(separator);
            List<string> lstStrs = new List<string>(strTmp.Length);
            foreach (string str in strTmp)
            {
                if (str != "")
                {
                    lstStrs.Add(str);
                }
            }
            return lstStrs;
        }

        /// <summary>
        /// 用,号拆分字符串
        /// </summary>
        /// <param name="strs"></param>
        /// <returns></returns>
        public static List<string> SplitToStr(string strs)
        {
            return SplitToStr(strs, ',');
        }


        #endregion

        #region 把ID和它的排序大小分开
        /// <summary>
        /// 把ID和它的排序大小分开
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="orders"></param>
        /// <param name="strOrders"></param>
        public static void SplitIDOrders(List<int> ids, List<int> orders, string strOrders)
        {
            string[] tmp = strOrders.Split(',');
            foreach (string str in tmp)
            {
                if (str != "")
                {
                    string[] strIDOrders = str.Split('|');
                    string id = strIDOrders[0];
                    string order = strIDOrders[1];
                    if (order == "")
                    {
                        order = "0";
                    }
                    ids.Add(int.Parse(id));
                    orders.Add(int.Parse(order));
                }
            }
        }
        #endregion

        #region 将Int型的集合转成指定分隔符分隔的字符串
        /// <summary>
        /// 将Int型的集合转成指定分隔符分隔的字符串
        /// </summary>
        /// <param name="lstIDs"></param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static string MergeToStr(IEnumerable<int> lstInt, string separator)
        {
            if (separator == null)
            {
                separator = ",";
            }
            StringBuilder sbInts = new StringBuilder();
            foreach (int id in lstInt)
            {
                sbInts.Append(separator + id);
            }
            if (sbInts.Length > 0)
            {
                sbInts.Remove(0, separator.Length);
            }
            string strInts = sbInts.ToString();
            return strInts;
        }
        #endregion

        #region 将string型的集合转成指定分隔符分隔的字符串
        /// <summary>
        /// 将string型的集合转成指定分隔符分隔的字符串
        /// </summary>
        /// <param name="lstStr"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string MergeToStr(IEnumerable<string> lstStr, string separator)
        {
            if (separator == null)
            {
                separator = ",";
            }
            StringBuilder sbStrs = new StringBuilder();
            foreach (string str in lstStr)
            {
                sbStrs.Append(separator + str);
            }
            if (sbStrs.Length > 0)
            {
                sbStrs.Remove(0, separator.Length);
            }
            string strs = sbStrs.ToString();
            return strs;
        }
        #endregion

        #region 截断字符串
        /// <summary>
        /// 截断字符串,英文两个算一个
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length">以中文长度为依据</param>
        /// <returns></returns>
        public static string Truncate(string str, int length)
        {
            return Truncate(str, length, null);
        }

        /// <summary>
        /// 截断字符串,英文两个算一个
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <param name="addition">如果被截断所需要加的字符串一般可为...</param>
        /// <returns></returns>
        public static string Truncate(string str, int length, string addition)
        {
            char[] chSrc = str.ToCharArray();
            int relLength = 0;//真正需要的长度
            int tmp = 0;
            foreach (char ch in chSrc)
            {
                if (tmp >= length * 2)//大于目标的双倍长度结束
                {
                    break;
                }
                if (ch > 255)
                {
                    tmp += 2;
                }
                else
                {
                    tmp += 1;
                }
                relLength++;
            }

            string strReturn = new string(chSrc, 0, relLength);
            if (relLength < str.Length && !string.IsNullOrEmpty(addition))
            {
                strReturn += addition;
            }

            return strReturn;
        }
        #endregion

        public static void ParseToParameter(string src, out string paraNames, out object[] paraValues)
        {
            string[] tmp = src.Trim(',').Split(',');
            paraNames = "";
            paraValues = new object[tmp.Length];
            for (int i = 0; i < tmp.Length; i++)
            {
                paraNames += ",@p" + i;
                paraValues[i] = tmp[i].Trim('\'');
            }
            paraNames = paraNames.Trim(',');
        }

        public static void ParseToParameter<T>(IList<T> src, out string paraNames, out object[] paraValues)
        {
            paraNames = "";

            paraValues = new object[src.Count];

            for (int i = 0; i < src.Count; i++)
            {
                paraNames += ",@p" + i;
                paraValues[i] = src[i];
            }
            paraNames = paraNames.Trim(',');
        }

    }
}
